package com.ara.common.kernel.base;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.StyleRes;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;
import androidx.viewbinding.ViewBinding;

import com.ara.common.kernel.R;
import com.ara.common.kernel.util.ScreenUtils;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;


/**
 * Created by XieXin on 2020/6/19.
 * 基础对话框
 */
public class BaseDialog<VB extends ViewBinding> extends Dialog {
    protected Context context;
    protected VB binding;
    protected Lifecycle lifecycle;

    public BaseDialog(@NonNull Context context, Lifecycle lifecycle) {
        super(context, R.style.DialogBgTranslucentStyle);
        this.context = context;
        this.lifecycle = lifecycle;
        this.binding = getViewBinding();
        bindToLifecycle(lifecycle);
    }

    public BaseDialog(@NonNull Context context, @StyleRes int themeResId, Lifecycle lifecycle) {
        super(context, themeResId);
        this.context = context;
        this.lifecycle = lifecycle;
        this.binding = getViewBinding();
        bindToLifecycle(lifecycle);
    }

    private VB getViewBinding() {
        try {
            //获取当前类的Type
            Type type = getClass().getGenericSuperclass();
            if (type instanceof ParameterizedType) {//判断是否支持泛型
                //获取泛型中的实例
                Class<VB> clazz = (Class<VB>) ((ParameterizedType) type).getActualTypeArguments()[0];
                //反射VB里面的inflate方法
                Method method = clazz.getMethod("inflate", LayoutInflater.class);
                //调用方法获得VB
                return (VB) method.invoke(null, getLayoutInflater());
            }
            return null;
        } catch (
                Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 绑定生命周期
     *
     * @param lifecycle Lifecycle
     */
    public void bindToLifecycle(Lifecycle lifecycle) {
        if (lifecycle == null) return;
        lifecycle.addObserver(new DefaultLifecycleObserver() {
            @Override
            public void onStop(@NonNull LifecycleOwner owner) {
                if (isShowing()) dismiss();
            }

            @Override
            public void onDestroy(@NonNull LifecycleOwner owner) {
                if (isShowing()) dismiss();
            }
        });
    }

    /**
     * 设置对话框宽度
     *
     * @param window  dialog 窗体
     * @param context 上下文
     */
    public static void setDialogWidth(Window window, Context context) {
        //获得窗体的属性
        if (window != null) {
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = ScreenUtils.getScreenWidth(context);
            if (ScreenUtils.isScreenOrientation(context)) {
                lp.width = lp.width - (lp.width / 2) - (lp.width / 10);
            } else {
                lp.width = lp.width - (lp.width / 6);
            }
            //将属性设置给窗体
            window.setAttributes(lp);
        }
    }

    /**
     * 设置对话框宽度
     *
     * @param window dialog 窗体
     * @param width  宽度
     */
    public static void setDialogWidth(Window window, int width) {
        //获得窗体的属性
        if (window != null) {
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = width;
            //将属性设置给窗体
            window.setAttributes(lp);
        }
    }
}
